package Options::Attributes::API::BasketValueUpdate;

use base qw (Exporter);
use strict;

use DE_EPAGES::Core::API::Log qw (LogDebug GetLog);
use DE_EPAGES::WebInterface::API::ApplicationServer qw (GetAppServer);
use DE_EPAGES::Presentation::API::Session
    qw( GetSessionAttribute SetSessionAttribute );
use DE_EPAGES::Customer::API::Shop qw (GetCustomerForUser);
use DE_EPAGES::Order::API::LineItemContainer qw(SavePayment SaveShipping);
use DE_EPAGES::Object::API::Factory
    qw( LoadObject ExistsObjectByPath LoadObjectByPath LoadClassByAlias);
use DE_EPAGES::Core::API::Error qw( Error ExistsError GetError );
use DE_EPAGES::WebInterface::API::ApplicationServer qw (GetAppServer);

use Data::Dumper;

sub UpdateLineItemsValue {
    my ($hParams) = @_;
    my $LineItemContainer = $hParams->{Object};
    if ( $LineItemContainer->get('RelativePath') =~ m/AnonymousUsers/ ) {
        GetLog->debug(
            "This is AnonymousUsers - creating discount line item skiped!");
        return;
    }
    else {

        GetLog->debug("HOOK UpdateLineItemsValue");
        my $count = 0;
        my $LineItemProductUpdate
            = $LineItemContainer->childrenByClass('LineItemProductUpdate');
        my $LineItemProducts
            = $LineItemContainer->childrenByClass('LineItemProduct');
        my $pricesRegular = {};
        my $pricesOptions = {};

        foreach my $LineItemProduct (@$LineItemProducts) {

            $pricesRegular->{ $LineItemProduct->get('ID') }
                = $LineItemProduct->get('LineItemPrice');
            $pricesOptions->{ $LineItemProduct->get('ID') }
                = $LineItemProduct->get('LineItemPriceFromOptions');
        }

        my $totalDiscount = 0;
        while ( my ( $OBID, $regularPrice ) = each(%$pricesRegular) ) {
            $totalDiscount
                = $pricesOptions->{$OBID} - $regularPrice + $totalDiscount;
        }

        #GLOBAL BASKET DISCOUNT IMPLEMENTATION START

        my $Basket               = $LineItemContainer->get('Parent');
        my $globalBasketDiscount = $Basket->get('TotalDiscountFromOptions');
        if ( $globalBasketDiscount > 0 ) {
            $totalDiscount = $totalDiscount - $globalBasketDiscount;
        }

        #STOP

        if ( ( scalar @$LineItemProducts ) > 0 ) {
            if ( ( scalar @$LineItemProductUpdate ) > 0 ) {

#If existing LineItemProductUpdate iterate and delete if more than 1,
                foreach
                    my $LineItemProductUpdateItem (@$LineItemProductUpdate)
                {
                    if ( $count > 0 ) {
                        $LineItemProductUpdateItem->delete;
                    }
                    else {
                        $LineItemProductUpdateItem->set(
                            {   'LineItemPrice' => $totalDiscount,
                                'Position'      => 10
                            }
                        );
                    }
                    $count++;
                }
            }
            else {

#If no existing LineItemProductUpdate create 1 and set its value to the LineItemProductUpdate
                $LineItemProductUpdate = $LineItemContainer->insertLineItem(
                    LoadClassByAlias('LineItemProductUpdate'),
                    {   'LineItemPrice' => $totalDiscount,
                        'Position'      => 10
                    }
                );
            }
        }
        return;
    }
}

1;
